package com.zx.web.service.impl;

import com.zx.bean.Address;
import com.zx.utils.DataSourceUtils;
import com.zx.web.dao.AddressDao;
import com.zx.web.dao.impl.AddressDaoImpl;
import com.zx.web.service.AddressService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.service.impl
 * @Description:
 * @Date 2022/9/15 10:56
 */
public class AddressServiceImpl implements AddressService {
    AddressDao dao = new AddressDaoImpl();
    @Override
    public List<Address> getAddressByUid(int userId) {
        return dao.getAddressByUid(userId);
    }

    @Override
    public boolean addAddress(Address address) {
        return dao.addAddress(address);
    }

    @Override
    public boolean deleteAddress(int id) {
        return dao.deleteAddress(id);
    }

    @Override
    public boolean updateAddress(Address address) {
        return dao.updateAddress(address);
    }

//    @Override
//    public Address findAddressLevel(int userId) {
//        return dao.findAddressLevel(userId);
//    }
//
//    @Override
//    public boolean updateAddressLevel(int id) {
//        return dao.updateAddressLevel(id);
//    }

    /**
     * 1.先全部设为0
     * 2.单独的设置为1
     * @param id
     * @param userId
     */
    @Override
    public void setDefaultAddress(int id, int userId) {
        Connection conn = null;
        try {
            conn = DataSourceUtils.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {

            //开启事务
            conn.setAutoCommit(false);

            //设置为普通的（全部地址）
            dao.setAllAdressNormal(userId);

//            System.out.println(1/0);

            //设置某地址为默认
            dao.setOneDefaultAddress(id);
            //提交事务
            conn.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {

        }
    }
}
